﻿Imports System.Windows.Forms
Imports System.Diagnostics

Public Class FirstTimeForm
    Dim rtm As RTMnet.RTM

    Private Sub FirstTimeForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        rtm = New RTMnet.RTM(My.Settings.ApiKey, sharedsecret)
        My.Settings.Frob = rtm.AuthGetFrob()
        Dim url = rtm.AuthCalcUrl(My.Settings.Frob, RTMnet.AuthLevel.Delete)
        Me.lblLink.Text = url
        Me.txtLink.Text = url


        'If String.IsNullOrEmpty(My.Settings.Token) OrElse IsLoggedIn() = False Then
        '    If MessageBox.Show("You are not logged in.  Would you like to login now?", "Login", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then

        '        Process.Start(url)

        '        MessageBox.Show("Your browser window should open shortly.  Please login and authorize the application the click Ok.", "Authorize", MessageBoxButton.OK, MessageBoxImage.None)
        '        'there is no token, so get one

        '        Dim auth = rtm.AuthGetToken(My.Settings.Frob)
        '        My.Settings.Token = auth.Token
        '        'End If
        '        My.Settings.Save()
        '    Else
        '        Exit Sub
        '    End If
        'End If
    End Sub

    Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
        Clipboard.SetText(Me.txtLink.Text)
    End Sub

    Private Sub lblLink_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lblLink.LinkClicked
        Process.Start(lblLink.Text)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click
        'test login information
        Try
            Dim auth = rtm.AuthGetToken(My.Settings.Frob)
            My.Settings.Token = auth.Token
            Dim myrtm As New RTMnet.RTM(My.Settings.ApiKey, sharedsecret, My.Settings.Token)
            myrtm.TestLogin()
            'Close window
            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        Catch ex As Exception
            MessageBox.Show("Application authorization failed.  Please follow the above steps and try again.", "Authorization Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class